package com.mc.lp.service;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.mc.lp.core.util.property.IPropertyUtil;
import com.mc.lp.database.cache.DBCacheService;
import com.mc.lp.database.service.handler.EventResponse;
import com.mc.lp.database.service.handler.IEventExecutor;
import com.mc.lp.database.service.handler.event.IEvent;
import com.mc.lp.database.service.handler.exception.EventHandlingException;
/**
 * every service must extend this class to act as a service in system.
 * @author deepak
 *
 * @param <T>
 */
public abstract class MicroCampusAbstractService<T extends IService> implements IService{
	
	private Class<T> clazz;
	
	@Resource
	public DBCacheService cacheService;
	
	@Resource
	public IEventExecutor eventExecutor;
	
	@Resource
	public IPropertyUtil propertyUtil;
	
	public Logger LOG =null;
	@SuppressWarnings("unchecked")
	public MicroCampusAbstractService(){
		Type type = getClass().getGenericSuperclass();
		ParameterizedType paramType = (ParameterizedType) type;
		clazz = (Class<T>) paramType.getActualTypeArguments()[0];
		setLogger();
	}
	
	private void setLogger(){
		LOG= Logger.getLogger(getClazz());
	}
	
	public void fireEvent(final IEvent event){
		getEventExecutor().fireEvent(event);
	}
	
	public EventResponse fireBlockerEvent(final IEvent event) throws EventHandlingException{
		return getEventExecutor().fireBlockerEvent(event);
	}
	
	public Class<T> getClazz() {
		return clazz;
	}
	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}

	public DBCacheService getCacheService() {
		return cacheService;
	}

	public void setCacheService(DBCacheService cacheService) {
		this.cacheService = cacheService;
	}

	public Logger getLogger() {
		return LOG;
	}

	public void setLogger(Logger lOG) {
		LOG = lOG;
	}

	public IEventExecutor getEventExecutor() {
		return eventExecutor;
	}

	public void setEventExecutor(IEventExecutor eventExecutor) {
		this.eventExecutor = eventExecutor;
	}

	public IPropertyUtil getPropertyUtil() {
		return propertyUtil;
	}

	public void setPropertyUtil(IPropertyUtil propertyUtil) {
		this.propertyUtil = propertyUtil;
	}

	public Logger getLOG() {
		return LOG;
	}

	public void setLOG(Logger lOG) {
		LOG = lOG;
	}
}